package com.byq.mm.abstractx.device;

import com.byq.mm.abstractx.identifier.IdentifierCreator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;



public class Device implements Serializable{
	String id;
	
	String name;
	Collection<Cluster> supportedClusters = new ArrayList<Cluster>();
	boolean gateway;
	Collection<Device> subdevices = new ArrayList<Device>();
	String transportId;
	public String getTransportId() {
		return transportId;
	}
	public void setTansportId(String transId) {
		this.transportId = transId;
		for(Device c : subdevices)
			c.setTansportId(transId);
	}
	public Collection<Device> getSubdevices() {
		return subdevices;
	}
	public void setSubdevices(Collection<Device> subdevices) {
		this.subdevices = subdevices;
	}
	public boolean isGateway() {
		return gateway;
	}
	public void setGateway(boolean gateway) {
		this.gateway = gateway;
	}
	
	public Collection<Cluster> getSupportedClusters() {
		return supportedClusters;
	}
	public void setSupportedClusters(Collection<Cluster> supportedClusters) {
		this.supportedClusters = supportedClusters;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	String localId;
	public String getLocalId() {
		return localId;
	}
	public void setLocalId(String localId) {
		this.localId = localId;
	}
	String type;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getGlobalId() {
		// TODO Auto-generated method stub
		return IdentifierCreator.createIdentifier(transportId, id);
	}
	public Collection<Device> getSubdevicesByType(String type){
		List<Device> rst = new ArrayList<Device>();
		for(Device c : subdevices){
			if(c.getType().equals(type))
				rst.add(c);
		}
		return rst;
	}
	public Collection<Device> getSubdevicesByCluster(String cluster){
		List<Device> rst = new ArrayList<Device>();
		for(Device c : subdevices){
			for(Cluster cl : c.getSupportedClusters()){
				if(cl.getId().equals(cluster)){
					rst.add(c);
					break;
				}
			}
		}
		return rst;
	}
	public Device getSubdeviceByType(String type){
		List<Device> rst = new ArrayList<Device>();
		for(Device c : subdevices){
			if(c.getType().equals(type))
				return c;
		}
		return null;
	}
}
